package de.tempel.pong.objects;

import java.awt.geom.Point2D;

import de.tempel.pong.objects.basic.AbstractGameObject;
import de.tempel.pong.objects.basic.Vector;

/**
 * Schläger
 * 
 * @author mathias
 * 
 */
public class Racket extends AbstractGameObject {

	/**
	 * Höhe des Spielfelds
	 */
	private final int windowHeight;
	private final int windowWidth;

	/**
	 * 
	 * @param position
	 * @param width
	 * @param heigth
	 * @param windowHeight
	 */
	public Racket(Point2D position, int width, int heigth, int windowWidth,
			int windowHeight) {
		super(position, width, heigth);
		this.windowHeight = windowHeight;
		this.windowWidth = windowWidth;
	}

	@Override
	public void move(float tpf) {

		Vector currentMovement = getMovement();
		Vector stepDirection = new Vector(currentMovement);
		stepDirection.multiply(tpf);

		Point2D newPosition = getPosition();
		newPosition = stepDirection.movePoint(newPosition);

		double newY = newPosition.getY();
		double newX = newPosition.getX();

		// Obere und untere Kand
		if (newY > 0 && newY + this.getHeight() < windowHeight) {

			// Rechts und links
			if (newX > 0 && newX + this.getWidth() < windowWidth) {
				this.setPosition(newPosition);
			} else {
				Vector newMovement = new Vector(0, currentMovement.getY());
				setMovement(newMovement);
			}

		} else {
			Vector newMovement = new Vector(currentMovement.getX(), 0);
			setMovement(newMovement);
		}
	}

	/**
	 * Bewegungsrichtung wird nach oben gesetzt.
	 */
	public void moveUp() {
		double xMovement = getMovement().getX();
		this.setMovement(new Vector(xMovement, -400d));
	}

	/**
	 * Bewegungsrichtung wird nach unten gesetzt.
	 */
	public void moveDown() {
		double xMovement = getMovement().getX();
		this.setMovement(new Vector(xMovement, +400d));
	}

	public void moveRight() {
		double yMovement = getMovement().getY();
		this.setMovement(new Vector(+400d, yMovement));
	}

	public void moveLeft() {
		double yMovement = getMovement().getY();
		this.setMovement(new Vector(-400d, yMovement));
	}

	/**
	 * Bewegungsrichtung wird auf 0 gesetzt.
	 */
	public void stopMoving() {
		this.setMovement(new Vector(0d, 0d));
	}

	@Override
	public String toString() {
		return "Racket [windowHeight=" + windowHeight + ", getPosition()="
				+ getPosition() + ", getHeight()=" + getHeight()
				+ ", getWidth()=" + getWidth() + "]";
	}

}
